package com.wanchuang.shengfubao.adapter;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.view.pinnedheader.PinnedHeaderAdapter;

import java.util.List;

public class PartnerAdapter extends PinnedHeaderAdapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_ITEM_TIME = 0;
    private static final int VIEW_TYPE_ITEM_CONTENT = 1;

    private List<String> mDataList;
    private OnItemClickListener clickListener;


    public PartnerAdapter(List<String> dataList, OnItemClickListener onItemClickListener) {
        mDataList = dataList;
        clickListener = onItemClickListener;
    }

    public void setData(List<String> dataList) {
        mDataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM_TIME) {
            return new TitleHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_partner_title, parent, false));
        } else {
            return new ContentHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_partner_content, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == VIEW_TYPE_ITEM_TIME) {
            TitleHolder titleHolder = (TitleHolder) holder;
            titleHolder.mTextTitle.setText(mDataList.get(position).substring(0, 1));
            int span = Integer.parseInt(mDataList.get(position).substring(1, 2));
            if (span == 0) {
                titleHolder.mTextTitle.setBackgroundResource(R.mipmap.bg_partnet);

            } else {
                titleHolder.mTextTitle.setBackgroundResource(R.mipmap.bg_partnet2);

            }
        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
            String[] strings = mDataList.get(position).split(" ");
            contentHolder.mTextTitle.setText(strings[0] + "  " + strings[1]);
            holder.itemView.setOnClickListener(v -> clickListener.onClick(v, strings[2]));
        }

    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mDataList.get(position).length() == 2) {
            return VIEW_TYPE_ITEM_TIME;
        } else {
            return VIEW_TYPE_ITEM_CONTENT;
        }
    }

    @Override
    public boolean isPinnedPosition(int position) {
        return getItemViewType(position) == VIEW_TYPE_ITEM_TIME;
    }

    static class ContentHolder extends RecyclerView.ViewHolder {

        TextView mTextTitle;

        ContentHolder(View itemView) {
            super(itemView);
            mTextTitle = itemView.findViewById(R.id.text_adapter_content_name);
        }
    }

    static class TitleHolder extends RecyclerView.ViewHolder {

        TextView mTextTitle;

        TitleHolder(View itemView) {
            super(itemView);
            mTextTitle = itemView.findViewById(R.id.text_adapter_title_name);
        }
    }

    /**
     * 监听输入框内容
     */
    public interface OnItemClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, String value);
    }


}
